﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using ARC.AliYun.SMSService.Interfaces;
using ARC.AliYun.SMSService.Models;
using ARC.AliYun.SMSService.Models.SimpleSmsModels;
using System.Collections.Generic;
using System.Text.Json;

namespace ARC.AliYun.SMSService.Service.SimpleSmsService
{
    public class SimpleSmsService : BaseAliyun, ISimpleSms
    {
        private CommonRequest _request;
        public SimpleSmsService(string accessKeyId, string secret)
        {
            _client = new DefaultAcsClient(DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, secret));//初始化阿里云控制客户端
            _request = CreateCommonRequest();
            _request.Method = MethodType.POST;
            _request.Domain = "dysmsapi.aliyuncs.com";
            _request.Version = "2017-05-25";
        }
        public AddAndDelSimpleSmsSingResponse AddSimpleSmsSign(string signName, string signSource, string remark, Dictionary<string, string> signFileList)
        {
            _request.Action = "AddSmsSign";
            _request.AddBodyParameters("SignName", signName);
            _request.AddBodyParameters("SignSource", signSource);
            _request.AddBodyParameters("Remark", remark);
            if (signFileList != null)
            {
                foreach (var item in signFileList)
                {
                    _request.AddBodyParameters("SignFileList.1.FileSuffix", item.Key);
                    _request.AddBodyParameters("SignFileList.1.FileContents", System.Web.HttpUtility.UrlEncode(item.Value));
                }
            }

            return Json<AddAndDelSimpleSmsSingResponse>(_request);
        }
        public QuerySimpleSmsSinResponse QuerySimpleSmsSign(string signName)
        {
            _request.Action = "QuerySmsSign";
            _request.AddQueryParameters("SignName", signName);
            return Json<QuerySimpleSmsSinResponse>(_request);
        }
        public AddAndDelSimpleSmsTemplateResponse AddSimpleSmsTemplate(int templateType, string templateName, string templateContent, string remark)
        {
            _request.Action = "AddSmsTemplate";
            _request.AddQueryParameters("TemplateType", templateType.ToString());
            _request.AddQueryParameters("TemplateName", templateName);
            _request.AddQueryParameters("TemplateContent", templateContent);
            _request.AddQueryParameters("Remark", remark);
            return Json<AddAndDelSimpleSmsTemplateResponse>(_request);
        }

        public AddAndDelSimpleSmsSingResponse DeleteSimpleSmsSign(string signName)
        {
            _request.Action = "DeleteSmsSign";
            _request.AddQueryParameters("SignName", signName);
            return Json<AddAndDelSimpleSmsSingResponse>(_request);
        }

        public AddAndDelSimpleSmsSingResponse ModifySimpleSmsSign(string signName, string signSource, string remark,
            Dictionary<string, string> signFileList)
        {
            _request.Action = "ModifySmsSign";
            _request.AddBodyParameters("SignName", signName);
            _request.AddBodyParameters("SignSource", signSource);
            _request.AddBodyParameters("Remark", remark);
            if (signFileList != null)
            {
                foreach (var item in signFileList)
                {
                    _request.AddBodyParameters("SignFileList.1.FileSuffix", item.Key);
                    _request.AddBodyParameters("SignFileList.1.FileContents", item.Value);
                }
            }

            return Json<AddAndDelSimpleSmsSingResponse>(_request);
        }
        public QuerySimpleSmsTemplateResponse QuerySimpleSmsTemplate(string templateCode)
        {
            _request.Action = "QuerySmsTemplate";
            _request.AddQueryParameters("TemplateCode", templateCode);
            return Json<QuerySimpleSmsTemplateResponse>(_request);
        }
        public AddAndDelSimpleSmsTemplateResponse DeleteSimpleSmsTemplate(string templateName)
        {
            _request.Action = "DeleteSmsTemplate";
            _request.AddQueryParameters("TemplateCode", templateName);
            return Json<AddAndDelSimpleSmsTemplateResponse>(_request);
        }

        public AddAndDelSimpleSmsTemplateResponse ModifySimpleSmsTemplate(int templateType, string templateName,
            string templateCode, string templateContent, string remark)
        {
            _request.Action = "ModifySmsTemplate";
            _request.AddQueryParameters("TemplateType", templateType.ToString());
            _request.AddQueryParameters("TemplateName", templateName);
            _request.AddQueryParameters("TemplateCode", templateCode);
            _request.AddQueryParameters("TemplateContent", templateContent);
            _request.AddQueryParameters("Remark", remark);
            return Json<AddAndDelSimpleSmsTemplateResponse>(_request);
        }
        public SendSmsResponse SendSimpleSms(string phoneNumbers, string signName, string templateCode, string templateParam)
        {
            _request.Action = "SendSms";
            _request.AddQueryParameters("PhoneNumbers", phoneNumbers);
            _request.AddQueryParameters("SignName", signName);
            _request.AddQueryParameters("TemplateCode", templateCode);
            _request.AddQueryParameters("TemplateParam", templateParam);
            return Json<SendSmsResponse>(_request);
        }

        public SendSmsResponse SendSimpleSmsBatch(List<string> phoneNumbers, List<string> signNameJson,
            string templateCode, List<Dictionary<string, string>> templateParam)
        {
            _request.Action = "SendBatchSms";
            _request.AddQueryParameters("PhoneNumberJson", JsonSerializer.Serialize(phoneNumbers));
            _request.AddQueryParameters("SignNameJson", JsonSerializer.Serialize(signNameJson));
            _request.AddQueryParameters("TemplateCode", templateCode);
            if (templateParam.Count != 0) _request.AddQueryParameters("TemplateParamJson", JsonSerializer.Serialize(templateParam));
            return Json<SendSmsResponse>(_request);
        }

        public QuerySendSmsDetails QueryDigitalSendDetails(string phoneNumber, string sendDate, int pageSize,
            int currentPage)
        {
            _request.Action = "QuerySendDetails";
            _request.AddQueryParameters("PhoneNumber", phoneNumber);
            _request.AddQueryParameters("SendDate", sendDate);
            _request.AddQueryParameters("PageSize", pageSize.ToString());
            _request.AddQueryParameters("CurrentPage", currentPage.ToString());
            return Json<QuerySendSmsDetails>(_request);
        }
    }
}
